package com.steven.im.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.steven.im.App;
import com.steven.im.R;
import com.steven.im.adapter.IMChatMsgAdapter;
import com.steven.im.base.BaseFragment;
import com.steven.im.entity.IMMsgList;
import com.steven.im.manager.DBManager;
import com.steven.im.ui.activity.ChatActivity;
import com.steven.im.utils.ScreenUtil;
import com.steven.im.widget.IMPopWindows;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class IMChatMsgFragment extends BaseFragment {
    @BindView(R.id.view_status_bar_place)
    View statusBarPlace;
    @BindView(R.id.title_left)
    ImageView titleLeft;
    @BindView(R.id.title_mid)
    TextView titleMid;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.mSwipeMenuListView)
    SwipeMenuListView mSwipeMenuListView;
    private List<IMMsgList> mList = new ArrayList<>();
    private IMChatMsgAdapter menuListAdapter;
    private boolean isFirst = true;//默认第一次加载

    @Override
    public View setContentView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_conversation, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setSwipeMenuListView();//设置 滑动删除
        ViewGroup.LayoutParams params = statusBarPlace.getLayoutParams();
        params.height = ScreenUtil.getStatusBarHeight(getContext());
        statusBarPlace.setLayoutParams(params);
        titleLeft.setVisibility(View.GONE);
        titleMid.setText("消息");
        ivTitleRight.setImageResource(R.drawable.icon_more1);

    }

    @OnClick({R.id.ll_right})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.ll_right:
                new IMPopWindows(getActivity(), ivTitleRight).showPop();
                break;
        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && isFirst) {
            isFirst = false;
            loadData();
        }
    }

    private void loadData() {
        mList.clear();
        mList.addAll(DBManager.chatMsgSelect());
        menuListAdapter = new IMChatMsgAdapter(getActivity(), LayoutInflater.from(getActivity()), mList);
        mSwipeMenuListView.setAdapter(menuListAdapter);
    }


    private void setSwipeMenuListView() {
        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem swipeMenuItem = new SwipeMenuItem(App.getContext());
                swipeMenuItem.setBackground(R.color.red);
                //swipeMenuItem.setTitleColor(R.color.white);
                swipeMenuItem.setTitleColor(Color.WHITE);
                swipeMenuItem.setWidth(dp2px());
                swipeMenuItem.setTitle("删除");
                swipeMenuItem.setTitleSize(14);
                menu.addMenuItem(swipeMenuItem);
            }
        };
        mSwipeMenuListView.setMenuCreator(swipeMenuCreator);
        mSwipeMenuListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
        //设置监听
        mSwipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        removeMsg();
                        mList.remove(position);
                        menuListAdapter.notifyDataSetChanged();
                        break;
                }
                return false;
            }
        });
        mSwipeMenuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击进入聊天界面
                startActivity(new Intent(getActivity(), ChatActivity.class));
            }
        });
        // set SwipeListener
        mSwipeMenuListView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {

            @Override
            public void onSwipeStart(int position) {
                // swipe start
            }

            @Override
            public void onSwipeEnd(int position) {
                // swipe end
            }
        });

        // set MenuStateChangeListener
        mSwipeMenuListView.setOnMenuStateChangeListener(new SwipeMenuListView.OnMenuStateChangeListener() {
            @Override
            public void onMenuOpen(int position) {
            }

            @Override
            public void onMenuClose(int position) {
            }
        });
    }

    /**
     * 删除聊天消息
     */
    private void removeMsg() {

    }

    private int dp2px() {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics());
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getActivity(), "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
